package connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class ConnectionManager {
	private Connection conexion;
	private String url = "";
	private String user = "";
	private String pass = "";

	/**
	 * @return la conexion
	 */
	public Connection getConexion() {
		return conexion;
	}

	public ConnectionManager() {
		ResourceBundle recurso = null;
		try {
			recurso = ResourceBundle.getBundle("App");
			if (recurso != null) {
				url = recurso.getString("conexion.url");
				user = recurso.getString("conexion.user");
				pass = recurso.getString("conexion.password");
			} else {
				System.out.print("NO CARGA DATOS DE CONEXION");
				System.exit(-1);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			if(this.conexion == null || this.conexion.isClosed()){
				this.conexion = DriverManager.getConnection(url, user, pass);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void open() throws SQLException{
		if(this.conexion == null || this.conexion.isClosed()){
			this.conexion = DriverManager.getConnection(url, user, pass);
		}		
	}
	public void close() throws SQLException{
		if(conexion != null || !conexion.isClosed())
		{
			conexion.close();
			conexion = null;			
		}		
	}
}
